﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using Microsoft.SqlServer.Dts.Runtime;

namespace SSISBatchGeocoder
{
    /// <summary>
    /// This class is used to generate the possible values for custom properties based on variables.
    /// </summary>
    public sealed class VariablesTypeConverter : TypeConverter
    {
        static StandardValuesCollection svc = new StandardValuesCollection(new ArrayList());

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        /// <summary>
        /// Builds a list of values based on the currently configured variables.
        /// </summary>
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            if (context == null) { return svc; }
            if (context.Instance == null) { return svc; }

            PropertyInfo propertyInfo = context.Instance.GetType().GetProperty("PipelineTask", typeof(TaskHost));

            if (propertyInfo == null) { return svc; }

            TaskHost host = propertyInfo.GetValue(context.Instance, null) as TaskHost;

            if (host == null) { return svc; }

            List<string> values = new List<string>();

            foreach (Variable v in host.Variables)
            {
                if (!v.SystemVariable)
                {
                    values.Add(v.QualifiedName);
                }
            }

            values.Sort();

            return new StandardValuesCollection(values);
        }
    }
}
